﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
namespace CungMuaC5.Products
{
    public partial class ThanhToan_4 : System.Web.UI.Page
    {
        BUS_DonHang donhang = new BUS_DonHang();
        BUS_KhuyenMai khuyenmai = new BUS_KhuyenMai();

        protected void Page_Load(object sender, EventArgs e)
        {
            //List<DTO_DonHang> danhsachdonhang = new List<DTO_DonHang>();
            //danhsachdonhang = donhang.SelectAll(Session.GetCurrentUser().MaTK.ToString());
            if (Session.GetDonHang() != null)
            {
                if (Request.QueryString["st"] == "Completed" || Request.QueryString["st"] == "Pending")
                {
                    DTO_DonHang dh = Session.GetDonHang();
                    dh.TinhTrangThanhToan = 1;
                    Session.SetDonHang(dh);
                    BUS_DonHang update = new BUS_DonHang();
                    update.UpdateTinhTrangThanhToan(dh.MaDH, 1);
                }

                List<DTO_DonHang> kq = new List<DTO_DonHang>();
                kq.Add(Session.GetDonHang());
                Session.SetDonHang(null);
                DetailsView1.DataSource = kq;
                DetailsView1.DataBind();

                List<DTO_KhuyenMai> sp = new List<DTO_KhuyenMai>();
                sp = khuyenmai.GetDetails(kq[0].KhuyenMai.ToString());
                Label tensp = (Label)DetailsView1.FooterRow.FindControl("Label1");
                tensp.Text = sp[0].TenKM;
                Label dongia = (Label)DetailsView1.FooterRow.FindControl("Label2");
                dongia.Text = sp[0].DonGiaMoi + " đ";
                Label soluong = (Label)DetailsView1.FooterRow.FindControl("Label3");
                Label thanhtien = (Label)DetailsView1.FooterRow.FindControl("Label4");
                string temp = null;
                for (int j = 0; j < dongia.Text.Length; j++)
                    if (dongia.Text[j] >= 48 && dongia.Text[j] <= 59)
                        temp += dongia.Text[j];
                thanhtien.Text = (Convert.ToInt32(temp) * Convert.ToInt32(soluong.Text)).ToString() + "đ";
                Label trangthai = (Label)DetailsView1.FooterRow.FindControl("Label7");
                switch (trangthai.Text)
                {
                    case "0":
                        trangthai.Text = "Chưa xử lý";
                        break;
                    case "1":
                        trangthai.Text = "Đã xử lý";
                        break;
                }
                Label thanhtoan = (Label)DetailsView1.FooterRow.FindControl("Label8");
                switch (thanhtoan.Text)
                {
                    case "0":
                        thanhtoan.Text = "Chưa thanh toán";
                        break;
                    case "1":
                        thanhtoan.Text = "Đã thanh toán";
                        break;
                }
                Label hinhthuc = (Label)DetailsView1.FooterRow.FindControl("Label9");
                switch (hinhthuc.Text)
                {
                    case "0":
                        if (kq[0].HinhThucNhanHang == 0)
                            hinhthuc.Text = "Thu tiền tại công ty";
                        else
                            hinhthuc.Text = "Thu tiền tại nhà";
                        break;
                    case "1":
                        hinhthuc.Text = "Thẻ ATM nội địa";
                        break;
                    case "2":
                        hinhthuc.Text = "Thẻ tín dụng";
                        break;
                }
                Label sum = (Label)DetailsView1.FooterRow.FindControl("Label6");
                Label phichuyen = (Label)DetailsView1.FooterRow.FindControl("Label5");
                temp = null;
                for (int j = 0; j < phichuyen.Text.Length; j++)
                    if (phichuyen.Text[j] >= 48 && phichuyen.Text[j] <= 59)
                        temp += phichuyen.Text[j];
                string temp1 = null;
                for (int j = 0; j < thanhtien.Text.Length; j++)
                    if (thanhtien.Text[j] >= 48 && thanhtien.Text[j] <= 59)
                        temp1 += thanhtien.Text[j];
                sum.Text = (Convert.ToInt32(temp1) + Convert.ToInt32(temp)).ToString() + "đ";


                DTO_NguoiNhan receiver = new DTO_NguoiNhan();

                receiver = Session.GetReceiver();
                Label13.Text = receiver.HoTen;
                Label14.Text = receiver.DienThoai;
                if (receiver.DiaChi != null)
                    Label15.Text = receiver.DiaChi;
                else
                    Label15.Text = "Lầu 2, 180-182 Lý Chính Thống, Q.3";
                HyperLink4.Text = receiver.Email;
                HyperLink4.NavigateUrl = "mailto:" + receiver.Email;
                Label16.Text = kq[0].GhiChu;
                Label17.Text = kq[0].MaDH.ToString();
            }
            else
                Response.Redirect("~/Products/Default.aspx");
            /*switch (PreviousPage.GetType().ToString())
            {
                case "ASP.products_thanhtoan_3_aspx":
                    receiver = Session.GetReceiver();
                    Label13.Text = receiver.HoTen;
                    Label14.Text = receiver.DienThoai;
                    if (receiver.DiaChi != null)
                        Label15.Text = receiver.DiaChi;
                    else
                        Label15.Text = "Lầu 2, 180-182 Lý Chính Thống, Q.3";
                    HyperLink4.Text = receiver.Email;
                    HyperLink4.NavigateUrl = "mailto:" + receiver.Email;
                    dh = Session.GetDonHang();
                    Label16.Text = dh.GhiChu;
                    Label17.Text = kq[0].MaDH.ToString();
                    break;
            }*/
        }
    }
}